package cc.mango.common;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import cc.telecomdigital.tdstock.CommonDefn.DoThingsReturn;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

public class HttpUtils
{
	public final static int	TIMEOUT_FOR_POST	= 3000;
	public final static int	TIMEOUT_FOR_GET	= 1000 * 60;
	private final static String TAG = HttpUtils.class.getSimpleName();
	
	
	public final static int	TIMEOUT_FOR_CONNECTION	= 1000 * 8;
	public final static int	TIMEOUT_FOR_READ	= 1000 * 10;
	
	public static InputStream urlToStream(final String theUrl)
			throws IOException
	{
		InputStream iStrm = null;
		URL url = new URL(theUrl);
		HttpURLConnection connection = (HttpURLConnection) url
				.openConnection();
		connection.setConnectTimeout(TIMEOUT_FOR_GET);
		connection.setReadTimeout(TIMEOUT_FOR_GET);
		connection.setRequestMethod("GET");
		connection
				.setRequestProperty(
						"User-Agent",
						"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2");
		//  connection.setRequestProperty("Referer",  "http://quotedev.telecomdigital.cc");  

		connection.setDoOutput(true);

		iStrm = connection.getInputStream();
		return iStrm;
	}

	public static InputStream urlToStreamWithPost(final String theUrl)
			throws IOException
	{
		InputStream iStrm = null;
		URL url = new URL(theUrl);
		HttpURLConnection connection = (HttpURLConnection) url
				.openConnection();
		connection.setRequestMethod("POST");
		connection
				.setRequestProperty(
						"User-Agent",
						"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2");
		//  connection.setRequestProperty("Referer",  "http://quotedev.telecomdigital.cc");  
		connection.setConnectTimeout(TIMEOUT_FOR_POST);
		connection.setReadTimeout(TIMEOUT_FOR_POST);
		connection.setUseCaches(false);
		connection.setDoOutput(true);

		iStrm = connection.getInputStream();
		return iStrm;
	}
	
	/*
	 * fix http head method issue 
	 */
	public static boolean  isServiceAvailable(final String url) 
	{
		final int defaultError   = -1;
		final DoThingsReturn doThingsReturn =  new DoThingsReturn(null,null,defaultError);
		try 
		{
			  // HttpURLConnection.setFollowRedirects(false);
			/*   HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
			   con.setRequestMethod("HEAD");
			   con.setFollowRedirects(false);

			   con.setConnectTimeout(TIMEOUT_FOR_CONNECTION); //set timeout to 5 seconds
			   con.setReadTimeout(TIMEOUT_FOR_READ);
			   return (con.getResponseCode() == HttpURLConnection.HTTP_OK);*/
			
			final DefaultHttpClient client = new DefaultHttpClient();
			HttpParams httpParams = client.getParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,
					TIMEOUT_FOR_CONNECTION);
			HttpConnectionParams.setSoTimeout(httpParams,
					TIMEOUT_FOR_READ);
			client.setParams(httpParams);
			HttpHead httpMethod = new HttpHead(url);
			try
			{
				client.execute(httpMethod, new ResponseHandler<String>()
				{
					public String handleResponse(HttpResponse response)
					{
						try {
							StatusLine status = response.getStatusLine();
							TDStockLog.d(TAG,  "url->" + url +  " ---statusCode - "+ status.getStatusCode());
							doThingsReturn.errCode = status.getStatusCode() ;
						} catch (Exception e) {
							doThingsReturn.errCode = defaultError;
						}
						return null;
						
					}
				});
			
			} 
			catch(Exception e) {
				return false;
			}
			
		
		}
		catch(Exception e) {
			return false;
		}	
		
		return doThingsReturn.errCode == 200 ;
	
	}
	

}